home *** CD-ROM | disk | FTP | other *** search
/ APDL Eductation Resources / APDL Eductation Resources.iso / programs / database / powerbase / docs / CSVfiles < prev    next >
Encoding:
Text File  |  1997-02-21  |  15.9 KB  |  282 lines

  1.                        CH 8 − USING CSV FILES
  2.                        ======================
  3. 8.1  What are CSV files?
  4. ========================
  5. CSV stands for “comma-separated values” and is the name given to data files
  6. in which each item of data is separated from the next by a comma. Such files
  7. are widely used to transfer data from one application to another, e.g. from
  8. a database to a spreadsheet or from a RISC OS database to a PC database. The
  9. following points should be noted:−
  10.  
  11. (1)     Data items which are non-numeric (e.g. plain text items such as
  12.         names and addresses) are often enclosed in double quotes (“”)
  13.         whereas numeric items are not. This makes it possible for an
  14.         application reading a CSV file to distinguish between numbers and
  15.         strings (which might of course contain numerals) and also allows a
  16.         comma to be used as a character within a string without being
  17.         mistaken for a data separator. (Addresses often contain commas e.g.
  18.         112, Keighley Road). For many purposes the quotes aren’t necessary
  19.         and may be omitted.
  20.  
  21. (2)     Null data items are usually included and can be located by looking
  22.         for two commas with either nothing in between them or with only two
  23.         quotation marks between them, i.e. ,, or ,“”,. Each line of data
  24.         (record) in such a file will always contain the same number of data
  25.         items (fields). Powerbase does, however, allow nulls to be omitted
  26.         entirely when creating a file in which case the number of fields per
  27.         record will vary.
  28.  
  29. (3)     The way in which each record is terminated varies from one system to
  30.         another. The last item of data in a record is not followed by a
  31.         comma but by a line terminator. On RISC OS systems this is the
  32.         line-feed character (ASCII value 10) and on PCs it is usually the
  33.         carriage-return character (ASCII value 13). You might, however,
  34.         encounter CSV files in which both these characters are used, i.e. LF
  35.         CR or CR LF.
  36.  
  37. (4)     Separator other than commas are sometimes used. The Tab character
  38.         (ASCII value 9) is often used and such files are called TSV files.
  39.         Powerbase allows you to separate data items and terminate lines (see
  40.         (3) above) with any character or character pair.
  41.  
  42. (5)     Some applications which accept a CSV file as input expect the first
  43.         line to contain the names of the fields which comprise the
  44.         subsequent records, e.g. if each record consists of a name and a
  45.         four-part address this header record might read:−
  46.  
  47.              “NAME”,“STREET”,“TOWN”,“COUNTY”,“POSTCODE”
  48.  
  49. Powerbase is able to save data in the form of true CSV files or files using
  50. another data separator. If the separator is a comma then the file created is
  51. of type &dfe and its icon displays the letters CSV. If another separator,
  52. eg. Tab, is used (see (4) above) then the file is of the ordinary Text type,
  53. i.e. &fff. Files of these types may also be used to enter data into a
  54. Powerbase database. In what follows we will, for convenience, refer to them
  55. all as “CSV files” whether or not the separator is a comma. The choices on
  56. the main menu which control these actions are Export CSV and CSV options.
  57.  
  58. 8.2  Setting the CSV options
  59. ============================
  60. The main-menu choice CSV options leads to a dialogue box which lets you
  61. specify all the file characteristics described earlier. Selection of the
  62. first three option buttons causes a saved file to have, respectively:−
  63.  
  64. •       quotes round non-numeric fields (see (1) above)
  65. •       a header record specifying the field names (see (5) above).
  66. •       null fields included (see (2) above).
  67.  
  68. Note that the field names referred to in (b) are, by default, the tags of
  69. the corresponding Powerbase fields, but may be changed to the descriptors by
  70. altering the setting in the Print options window. The field separator and
  71. record terminator may be changed by means of menus which appear when you
  72. click on the pop-up menu icons. Each of these menus includes a writable
  73. entry allowing you to define your own separator and/or terminator of one or
  74. two printing characters.
  75.  
  76. 8.3  Exporting data as a CSV file
  77. =================================
  78. Having set up your options as described above, creating a CSV file is very
  79. like printing a list. First highlight the fields to be exported by clicking
  80. on each with ADJUST. Remember that the order in which the fields are
  81. highlighted is important. Next choose Export CSV from the main menu. A
  82. window appears featuring the Query panel. Type in the search formula to
  83. determine which records are exported. Finally, enter the name of the file
  84. and drag the file icon to a filer window. By default the file is saved in
  85. PrintJobs as usual, and you may simply click on Export or type Return.
  86. Experiment with saving CSV files with different settings of the CSV options
  87. and then loading the resulting files into Edit to examine them.  If you
  88. select the Reverse switch on the Query panel the CSV file will be created in
  89. reverse order. (See also 3.3.5)
  90.  
  91. 8.4  Using CSV files to import data
  92. ===================================
  93. If you drag a CSV file onto the record display of an open database the
  94. options window (see CSV options) appears with a changed title and some
  95. additional icons, one of which displays the pathname of the CSV file.
  96. Clicking on Import will make Powerbase try to create new database records
  97. from the file. If you decide not to do this you should click on Cancel. If
  98. you do wish to import the data there are some important consideration which
  99. will now be explained.
  100.  
  101. 8.4.1 Ensuring that the correct options are selected
  102. ----------------------------------------------------
  103. If the CSV file originally came from a Powerbase application the settings in
  104. the CSV options window should be exactly the same as they were when the file
  105. was exported. The exception to this is the Quotes button which is shaded on
  106. import because Powerbase doesn’t need it. If the CSV file came from a PC or
  107. another RISC OS application you might have to load it into Edit to find out
  108. what separator and terminator are used.
  109.  
  110. There are three more option switches at the bottom of the window. One causes
  111. each record to be displayed as it is imported. The import process is slower
  112. with this turned on but much more informative if you like to know how things
  113. are progressing. The second switch, when selected, strips any trailing
  114. spaces in the imported data-fields. Some database programs pad all fields to
  115. their maximum length by adding spaces to the end of the data where
  116. necessary.  If you import such a file into Powerbase you will find that the
  117. caret will always be at the far right of the field even though visible
  118. characters do not fill the field, and some queries won’t work properly.
  119. Setting the Strip spaces switch before importing the file overcomes the
  120. problem.  The third option switch determines how Sequence number fields are
  121. handled.  If the switch is ON imported sequence numbers are ignored and new
  122. ones assigned in accordance with the field’s sequence number counter.  With
  123. the switch OFF sequence numbers from the CSV file are imported without
  124. alteration.
  125.  
  126. 8.4.2 Directing imported data to the correct fields
  127. ---------------------------------------------------
  128. If no fields on the record screen are highlighted (i.e. with ADJUST) and the
  129. CSV file does not contain a header record (see 8.1) then the import process
  130. proceeds according to the following rules:−
  131.  
  132. •       The first CSV field will be read into the first Powerbase field for
  133.         which importing is allowed (Graphics fields, Buttons or fields which
  134.         are merely labels will be ignored). The next CSV field will be read
  135.         into the second Powerbase field and so on.
  136.  
  137. •       If the end of the CSV record is reached before all the relevant
  138.         fields have been filled (data underflow) then the next CSV record
  139.         will start a new Powerbase record, i.e. the reading won’t get out of
  140.         step. It does not matter, therefore, if the CSV file omits null
  141.         items at the end of a line.
  142.  
  143. •       If all relevant fields are filled before reaching the end of the CSV
  144.         record (data overflow) Powerbase ignores the remainder of the line
  145.         and skips to the beginning of the next CSV record before starting a
  146.         new Powerbase record. This is also to keep the operation in step.
  147.  
  148. There will be occasions when you don’t want to fill the Powerbase fields
  149. sequentially as just described. There are two ways of making the process
  150. more specific:−
  151.  
  152. •       Highlight the required Powerbase fields with ADJUST before starting
  153.         the import.  Data will then be read only into the highlighted
  154.         fields, all other fields being ignored. The order in which the
  155.         fields are filled is the order in which you highlighted them.  The
  156.         rules given above about underflow and overflow of data still apply.
  157.  
  158. •       Give the file a header record containing the tags or descriptors of
  159.         required fields in the Powerbase record.  (There is nothing to
  160.         prevent you manually adding such a header to a CSV file which did
  161.         not originate from a Powerbase application.) Importing then occurs
  162.         just as if those fields were highlighted. The Print options window
  163.         must reflect whether the tags or descriptors of fields are used.
  164.  
  165. Do not use both a header and highlighting.
  166.  
  167. 8.4.3 Importing plain text files
  168. --------------------------------
  169. It was previously explained that files created with Export CSV can have
  170. separators other than a comma. Such files will be of type &fff (plain text)
  171. instead of &dfe (CSV).  They can still be imported into a Powerbase database
  172. but a certain amount of caution is needed because there are other
  173. circumstances in which a text file might be dropped on the record window. A
  174. properly-written script file (see Ch 9) would be recognised as such and
  175. therefore cause no problem, but any text file dropped onto the appropriate
  176. type of External field (i.e. a Text or Text Block) field would become linked
  177. to that field instead of being treated like a CSV file. If you are importing
  178. data from a plain text file and your record contains fields of the
  179. aforementioned types be sure to drop the file on the window background, not
  180. on the External field. You are strongly advised to use proper CSV files if
  181. at all possible.
  182.  
  183. 8.4.4 What if the imported data won’t fit?
  184. ------------------------------------------
  185. There are two situations in which this can happen. The database might not
  186. contain enough free records to hold all the imported data and so you get a
  187. “Database full when reading CSV file” error. To avoid this either make sure
  188. the database is big enough before you start or place a suitable value in the
  189. Increment for expansion icon in the Change length window.  The latter is
  190. accessible from the Utilities submenu of the icon-bar menu.
  191.  
  192. The second situation is where an item is too long for the destined database
  193. field. When importing data Powerbase maintains a file called TooBig inside
  194. the database’s PrintJobs directory. Anything which won’t fit in the target
  195. field is written to this file together with information about where it was
  196. intended to go. No writable Powerbase field may be longer than 246
  197. characters and if an item of imported data exceeds this a note will be made
  198. in the TooBig file advising you to define an External field (Text Block or
  199. Text) for such data.
  200.  
  201. 8.5 Using CSV files to modify existing records
  202. ==============================================
  203. Most database programs which support CSV import allow it to be used only for
  204. creating new records.  Powerbase is unusual in that you can use a CSV file
  205. to modify existing records.  This capability should be used with caution
  206. since careless use can irrevocably garble a database.  There are three
  207. relevant radio buttons in the CSV options window.  They are called Modify
  208. existing, With primary key and With rec. number and their actions are as
  209. follows:
  210.  
  211. Modify existing
  212. ---------------
  213. No new records will be created when a CSV file is dropped onto the record
  214. window.  The existing records will be accessed in the order determined by
  215. the current index and the new data will be merged into these records.  You
  216. should, of course, either use ADJUST to highlight the fields into which the
  217. data should go or place a header in the CSV file specifying the field tags
  218. and then turn on the With header switch (see 8.4.2).  If all the records are
  219. modified before the end of the CSV file has been reached a warning message
  220. is displayed.  Note that it is the user’s responsibility to ensure that the
  221. data in the CSV file is in the correct order since Powerbase has no way of
  222. telling which data is destined for which record and can only proceed
  223. sequentially.  As an additional precaution a warning message is displayed
  224. when you choose this option.
  225.  
  226. With primary key
  227. ----------------
  228. This affects both export and import.  When you export data with this button
  229. selected  each record of the CSV file includes the primary key of the
  230. Powerbase record. (Try creating a CSV file with and without this option set
  231. and compare the files using Edit.)  When importing such a file Powerbase
  232. will attempt to locate records with the same primary keys as the records in
  233. the CSV file. If a matching key is not found a new record will be created to
  234. receive the data. If it is found then the CSV data goes into the same
  235. record, overwriting any data which the target fields already contain.  The
  236. option is only useful for transferring data between Powerbase databases
  237. which have the same primary key field.  Don’t forget that Powerbase, by
  238. default, allows duplicate primary keys so it is possible for the new data
  239. going into the wrong record.  You would be ill-advised to trust this method
  240. of import unless you are sure each primary key is unique.
  241.  
  242. With record number
  243. ------------------
  244. This resembles the previous option. It allows data to be exported with the
  245. Powerbase record numbers included. Importing into another (or the same)
  246. database places the data in records having the same record numbers, again
  247. overwriting data which may already be present in the target fields.  This
  248. option is only useful for transferring data between Powerbase databases in
  249. which corresponding records have identical record numbers.  One use for it
  250. might be to export a set of data, load it into an editor and do some
  251. extensive searching-and-replacing, then put the modified data back into the
  252. original database.
  253.  
  254. 8.6 Creating a new, working database from a CSV file
  255. ====================================================
  256. There is an option button in the CSV options window called With field data
  257. which only becomes active when the With header button is selected. With the
  258. switch selected the header record of a saved CSV file contains not only the
  259. field names but also the field lengths and types. Again it is suggested that
  260. you create a file and look at it in Edit. Each item in the header begins
  261. with a number. This is the field length in characters, i.e. the maximum
  262. allowed length in the Powerbase record field. Next comes a ¤ character which
  263. separates the length from the field name. Another ¤ separates the field name
  264. from the concluding number which determines the field type. (The type
  265. numbers may be inspected by looking at the ValStrings file in the Powerbase
  266. directory.)
  267.  
  268. A file created in this form is not meant to be dropped onto the window of an
  269. open database. It should be dropped onto the Powerbase icon on the icon-bar
  270. when no database is open. Powerbase will attempt to convert the file into a
  271. functioning database. All the fields will be ranged on the left of the
  272. record window, one beneath another and the primary key will consist of the
  273. first four characters of the first field. Don’t try to create databases
  274. containing Check box, Button or External fields using this method; it’s only
  275. meant for the most basic type of database.
  276.  
  277. 8.7  CSV files and validation tables
  278. ====================================
  279. You can save the contents of a validation table as a CSV file (see 5.6).
  280. You can also import data into a validation table by dropping a CSV file onto
  281. it, the procedure being just the same as that described for importing into
  282. database records.